package com.cooper.controllers.admin;

import java.sql.Timestamp;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.controllers.Template;
import com.cooper.core.JsonResult;
import com.cooper.entity.BrandBaseEntity;
import com.cooper.service.admin.BrandManageService;
import com.cooper.util.Controllers;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.portal.Portal;


@AdminRequired
public class BrandController {

	@Autowired
	BrandManageService brandManageService;
	
	@Template(title="品牌管理")
	@Get
	public String content(Invocation inv, Portal portal) throws Exception{
		BrandBaseEntity brandBaseDo = new BrandBaseEntity();
		inv.addModel("dataList", brandManageService.findBrandList(brandBaseDo));
		return "BrandList";
	}
	
	@Get("add")
	public String addBrand(Invocation inv){
		return "BrandAdd";
	}
	
	@Post("doAdd")
	public Object doAddBrand(Invocation inv,BrandBaseEntity brand){
		JsonResult json = new JsonResult();
		try{
			if(StringUtils.isEmpty(brand.getLogoUrl())){
				json.setSuccess(false);
				json.setMessage("品牌添加失败");
			} else {
				brandManageService.addBrand(brand);
				json.setSuccess(true);
			}
		}catch(Exception e){
			json.setSuccess(false);
		}
		
		return json;
	}
	
	
	@Post("delete")
	public Object deleteBrand(Invocation inv){
		JsonResult json = new JsonResult();
		String brandId = inv.getParameter("brandId");
		if(StringUtils.isEmpty(brandId)){
			json.setSuccess(false);
		} else {
			
			BrandBaseEntity bdo = brandManageService.findBrandById(new Long(brandId));
			if(bdo!=null){
				bdo.setModifier(Controllers.getUserInfo(inv).getUserName());
				bdo.setGmtModified(new Timestamp(System.currentTimeMillis()));
				brandManageService.deleteBrand(bdo);
			}
			json.setSuccess(true);
		}
		return json;
	}
}
